﻿@using Sovigaz
@{
    ViewBag.Title = "Chi tiết lô";
    var url = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/');
}
<script>
    app.controller('LotController', function ($scope, $compile, $http, $filter, service) {    
        $scope.load = function () {
            startloading($('#content'));

            var url = '@url' + '@Url.Action("LotApi", "Api", new {id = Model})';

            $http.get(url)
                .success(function (data) {
                    $scope.lot = data;
                    $scope.status = { name: '', value: data.Status };
                    stoploading($('#content'));
                })
                .error(function (data) {
                    stoploading($('#content'));
                    service.showModal($scope, $compile, 'error', data);
                });
        };

        $scope.refresh = function () {
            $scope.load();
        };

        $scope.add = function () {
            window.location = '@Url.Action("Detail", "Lot", new { id = String.Empty })';
        };

        $scope.save = function () {
            $scope.lot.Status = $scope.status.value;
            $scope.lot.Date = $filter('date')(Date.parse($scope.lot.Date), 'y/MM/dd');
            var url = '@url' + '@Url.Action("LotApi", "Api")';
            $http.put(url, $scope.lot)
                .success(function(data) {
                    if (@Model == 0) {
                        window.location = '@Url.Action("Detail", "Lot")/' + data.LotID;
                    }
                    $scope.lot = data;

                    stoploading($('#content'));
                    service.showModal($scope, $compile, 'notify', 'Dữ liệu đã được lưu thành công!!!');
                })
                .error(function(data) {
                    stoploading($('#content'));
                    service.showModal($scope, $compile, 'error', data);
                });
        };

        $scope.confirmDelete = function () {
            service.showModal($scope, $compile, 'confirm', 'Bạn có chắc chắn muốn xóa dữ liệu?', 'delete()');
        };

        $scope.delete = function () {
            var url = '@url' + '@Url.Action("LotApi", "Api")';
            $http.delete(url, {
                params: {
                    id: $scope.lot.LotID
                }
            }).success(function (data) {
                stoploading($('#content'));
                window.location = '@Url.Action("Detail", "Lot", new { id = String.Empty })';
            }).error(function (data) {
                stoploading($('#content'));
                service.showModal($scope, $compile, 'error', data);
            });
        };

        $scope.back = function () {
            window.location = '@Url.Action("Index", "Lot")';
        };

        $scope.statuslist = [
            { name: 'Mới', value: 'N' },
            { name: 'Đã duyệt', value: 'C' }
        ];

        $scope.status = {};
        $scope.load();
    });

    $(function () {
        $("[data-toggle=tooltip]").tooltip();
    });
</script>

<div ng-controller="LotController">
    @Html.Partial("_PageHeader")
    <div class="row">
    <div class="toolbar">
        @if (Utilities.ConvertUtility.BoolParse(ViewBag.InsertPermission))
        {
            <button class="btn btn-primary btn-sm" type="button" ng-click="add()">
                <i class="fa fa-plus fa-fw"></i> Thêm mới
            </button>
        }
        <button class="btn btn-primary btn-sm" type="button" ng-click="save()">
                <i class="fa fa-print fa-fw"></i> In
            </button>   
        @if (Utilities.ConvertUtility.BoolParse(ViewBag.InsertPermission))
        {
            <button class="btn btn-primary btn-sm" type="button" ng-click="save()">
                <i class="fa fa-floppy-o fa-fw"></i> Lưu
            </button>            
        }        
        @if (Utilities.ConvertUtility.BoolParse(ViewBag.BackEnable))
        {
            <button class="btn btn-primary btn-sm" type="button" ng-click="back()">
                <i class="fa fa-chevron-circle-left fa-fw"></i> Trở lại
            </button>
        }
    </div>
</div>
    <div id="content" class="content row">
        <form role="form" name="form" autocomplete="off">
            <div class="panel-body">
                <div class="col-lg-4">
                    @Html.Partial("Control/_Control_TextBox_Disabled", new Dictionary<string, string>
                    {
                        {"Text", "Mã số lô"},
                        {"Model", "lot.LotCode"},
                        {"Name", "lotCode"},
                    })
                </div>
                <div class="col-lg-4" style="clear: both">
                    @Html.Partial("Control/_Control_Datepicker", new Dictionary<string, string>
                    {
                        {"Text", "Ngày tạo"},
                        {"Model", "lot.Date"},
                        {"Name", "date"}
                    })
                </div>
                <div class="col-lg-8" style="clear: both">
                    @Html.Partial("Control/_Control_TextArea", new Dictionary<string, string>
                    {
                        {"Text", "Ghi chú"},
                        {"Model", "lot.Description"},
                        {"Name", "description"}
                    })
                </div>
                <div class="col-lg-4" style="clear: both">
                    @Html.Partial("Control/_Control_ComboBox", new Dictionary<string, string>
                    {
                        {"Text", "Tình trạng"},
                        {"Model", "status"},
                        {"Name", "status"},
                        {"Options", "status.name for status in statuslist track by status.value"}
                    })
                </div>
            </div>
        </form>
    </div>
</div>